/*
 * InDir1Block.cpp
 *
 *  Created on: Apr 19, 2011
 *      Author: Stefan
 */
#include <iostream>

#include "InDir1Block.h"
#include "FS.h"

InDir1Block::InDir1Block(int number, byte* data) : InDirBlock(number, data) {
	for (int i = 0; i < 128; i++){
		addresses[i] = readLong(i*4);
	}
}

InDir1Block::~InDir1Block() {
//	delete [] addresses;
}

void InDir1Block::addAddress(long number, int index){
	if (index >= 0 && index < 128){
		addresses[index] = number;
		write(index, addresses[index]);
	}
}

void InDir1Block::removeAddress(int index){
	if (index >= 0 && index < 128){
		addresses[index] = 0;
		write(index, addresses[index]);
	}
}

long InDir1Block::getAddress(int index){
	if (index >= 0 && index < 128){
		return addresses[index];
	}
	return -1;
}

void InDir1Block::print(){
	for(int i = 0; i < 128; i++){
		cout << " " << addresses[i];
	}
}

void InDir1Block::free(){
	FS* fs = FS::getInstance();
	for(int i = 0; i < 128; i++){
		if(addresses[i] != 0){
			fs->addFreeBlock(addresses[i]);
		}
	}
}
